package com.mogujie.tt.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class GETUtil {
	 /**
     * 向指定URL发送GET方法的请求
     * 
     * @param url
     *            发送请求的URL
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL 所代表远程资源的响应结果
     */
     private  String url="http://qz.atmart.cn:4024/index.php";
     private static GETUtil getUtil=null;

     public static GETUtil instance(){
        if(getUtil ==null){
            synchronized (GETUtil.class){
                getUtil = new GETUtil();
            }
        }
        return getUtil;
     }

     public String sendGet(String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            System.out.println("1111111" + urlNameString);
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "text/css; charset: utf-8");
            connection.setRequestProperty("Content-Encoding", "gzip");
            connection.setRequestProperty("X-Powered-By", "PHP/5.3.28");
            //connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                //System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
         System.out.println("111111" + result);
        return decodeUnicode(result);
    }
    public  String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = theString.charAt(x++);
                if (aChar == '\\') {
                  aChar = theString.charAt(x++);
                    if (aChar == 'u') {
                        // Read the xxxx
                         int value = 0;
                        for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                switch (aChar) {
                    case '0':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                    value = (value << 4) + aChar - '0';
                    break;
                    case 'a':
                    case 'b':
                    case 'c':
                    case 'd':
                    case 'e':
                    case 'f':
                    value = (value << 4) + 10 + aChar - 'a';
                    break;
                    case 'A':
                    case 'B':
                    case 'C':
                    case 'D':
                    case 'E':
                    case 'F':
                    value = (value << 4) + 10 + aChar - 'A';
                    break;
                    default:
                    throw new IllegalArgumentException(
                     "Malformed   \\uxxxx   encoding.");
                      }
                    }
                     outBuffer.append((char) value);
                    } else {
                     if (aChar == 't')
                          aChar = '\t';
                    else if (aChar == 'r')
                    aChar = '\r';
                    else if (aChar == 'n')
                    aChar = '\n';
                    else if (aChar == 'f')
                      aChar = '\f';
                     outBuffer.append(aChar);
                }
               } else
           outBuffer.append(aChar);
          }
          return outBuffer.toString();
         }



    public String getRequestText(String app, String mod, String act,String login_name,String login_password,String feed_id
            ,String type,String ince_id,String max_id,String limit,String page,String app_name){
        StringBuffer buffer = new StringBuffer();
        buffer.append("app="+app);
        buffer.append("&");
        buffer.append("mod="+mod);
        buffer.append("&");
        buffer.append("act="+act);
        buffer.append("&");
        buffer.append("login_name="+login_name);
        buffer.append("&");
        buffer.append("login_password="+login_password);
        if (feed_id!=null){
            buffer.append("&");
            buffer.append("feed_id="+feed_id);
        }
        if (type!=null){
            buffer.append("&");
            buffer.append("type="+type);
        }
        if (ince_id!=null){
            buffer.append("&");
            buffer.append("ince_id="+ince_id);
        }
        if (max_id!=null){
            buffer.append("&");
            buffer.append("max_id="+max_id);
        }
        if (limit!=null){
            buffer.append("&");
            buffer.append("limit="+limit);
        }
        if (page!=null){
            buffer.append("&");
            buffer.append("page="+page);
        }
        if (app_name!=null){
            buffer.append("&");
            buffer.append("app_name="+app_name);
        }
        String v = buffer.toString();
        //System.out.println(v);
        return v;
    }

    public String getRequestText1(String app, String mod, String act,String login_name,String login_password,String app_name,String table_name
            ,String app_uid,String row_id,String to_comment_id,String to_uid,String content){
        StringBuffer buffer = new StringBuffer();
        buffer.append("app="+app);
        buffer.append("&");
        buffer.append("mod="+mod);
        buffer.append("&");
        buffer.append("act="+act);
        buffer.append("&");
        buffer.append("login_name="+login_name);
        buffer.append("&");
        buffer.append("login_password="+login_password);
        if (app_name!=null){
            buffer.append("&");
            buffer.append("app_name="+app_name);
        }
        if (table_name!=null){
            buffer.append("&");
            buffer.append("table_name="+table_name);
        }
        if (app_uid!=null){
            buffer.append("&");
            buffer.append("app_uid="+app_uid);
        }
        if (row_id!=null){
            buffer.append("&");
            buffer.append("row_id="+row_id);
        }
        if (to_comment_id!=null){
            buffer.append("&");
            buffer.append("to_comment_id="+to_comment_id);
        }
        if (to_uid!=null){
            buffer.append("&");
            buffer.append("to_uid="+to_uid);
        }
        if (content!=null){
            buffer.append("&");
            buffer.append("content="+content);
        }
        String v = buffer.toString();
        return v;
    }
    public String getRequestText3(String app, String mod, String act,String login_name,String login_password,String type,String body,
                                  String app_name,String channel,String content,String attach_id,String video_id,String videourl,String channel_id){
        StringBuffer buffer = new StringBuffer();
        buffer.append("app="+app);
        buffer.append("&");
        buffer.append("mod="+mod);
        buffer.append("&");
        buffer.append("act="+act);
        buffer.append("&");
        buffer.append("login_name="+login_name);
        buffer.append("&");
        buffer.append("login_password="+login_password);
        if (type!=null){
            buffer.append("&");
            buffer.append("type="+type);
        }
        if (body!=null){
            buffer.append("&");
            buffer.append("body="+body);
        }
        if (app_name!=null){
            buffer.append("&");
            buffer.append("app_name="+app_name);
        }
        if (channel!=null){
            buffer.append("&");
            buffer.append("channel="+channel);
        }
        if (content!=null){
            buffer.append("&");
            buffer.append("content="+content);
        }
        if (attach_id!=null){
            buffer.append("&");
            buffer.append("attach_id="+attach_id);
        }
        if (video_id!=null){
            buffer.append("&");
            buffer.append("video_id="+video_id);
        }
        if (videourl!=null){
            buffer.append("&");
            buffer.append("videourl="+videourl);
        }
        if (channel_id!=null){
            buffer.append("&");
            buffer.append("channel_id="+channel_id);
        }
        String v = buffer.toString();
        return v;
    }
    public String getRequestText4(String app, String mod, String act,String login_name,String login_password,String comment_id){
        StringBuffer buffer = new StringBuffer();
        buffer.append("app="+app);
        buffer.append("&");
        buffer.append("mod="+mod);
        buffer.append("&");
        buffer.append("act="+act);
        buffer.append("&");
        buffer.append("login_name="+login_name);
        buffer.append("&");
        buffer.append("login_password="+login_password);
        if (comment_id!=null){
            buffer.append("&");
            buffer.append("comment_id="+comment_id);
        }
        String v = buffer.toString();
        return v;
    }

    public String getRequestText5(String app, String mod, String act,String login_name,String login_password,String type,String uid,String uname
            ,String since_id,String max_id,String limit,String page){
        StringBuffer buffer = new StringBuffer();
        buffer.append("app="+app);
        buffer.append("&");
        buffer.append("mod="+mod);
        buffer.append("&");
        buffer.append("act="+act);
        buffer.append("&");
        buffer.append("login_name="+login_name);
        buffer.append("&");
        buffer.append("login_password="+login_password);
        if (type!=null){
            buffer.append("&");
            buffer.append("type="+type);
        }
        if (uid!=null){
            buffer.append("&");
            buffer.append("uid="+uid);
        }
        if (uname!=null){
            buffer.append("&");
            buffer.append("uname="+uname);
        }
        if (since_id!=null){
            buffer.append("&");
            buffer.append("since_id="+since_id);
        }
        if (max_id!=null){
            buffer.append("&");
            buffer.append("max_id="+max_id);
        }
        if (limit!=null){
            buffer.append("&");
            buffer.append("limit="+limit);
        }
        if (page!=null){
            buffer.append("&");
            buffer.append("page="+page);
        }
        String v = buffer.toString();
        //System.out.println(v);
        return v;
    }

}
